package com.zzyl.job;

import com.zzyl.entity.Reservation;
import com.zzyl.mapper.ReservationMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 预约过期任务
 */
@Component
@Slf4j
public class ReservationJob {

    @Autowired
    private ReservationMapper reservationMapper;

    @Scheduled(cron = "0 0/30 8-18 * * ?")      // 秒  0分开始30分钟为周期 8-18小时 天 月 ?星期
    public void checkReservationTimeOut(){
        log.info("开始检查预约是否过期:{}", LocalDateTime.now());

        List<Reservation> timeoutReservation = reservationMapper.getTimeoutReservation(LocalDateTime.now());

        timeoutReservation.forEach(reservation -> {
            reservation.setStatus(3);   //状态该过期
            reservation.setUpdateTime(LocalDateTime.now());  // 更新时间
            reservation.setRemark("预约超时，系统自动取消");
            reservationMapper.update(reservation);
        });

    }
}
